import requests

def account_info(key, account):
    """
    查询账号基本信息
    ex:account_info('104255897823')
    :param account: account id
    :return: account user information
    """

    try:
        url = 'https://api.newrank.cn/api/sync/douyin/account/info/v2'
        layout = {'account': f'{account}'}
        header = {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
            "Key": key}
        resp = requests.post(url, data=layout, headers=header)
        return resp.json()
    except Exception as e:
        print(e)


def douyin_day_top50(key, search_type, search_date):
    """
    获取指定新榜类别的日榜TOP50（仅可查2019年11月11号之前的日榜榜单）
    ex:douyin_day_top50('家居','2022-01-13')
    :param douyin_type:娱乐、才艺、萌宠、搞笑、二次元、游戏、家居、美食、旅游、健康、企业、体育、教育、科技、汽车、情感、时尚、文化、社会
    :param search_date:'2022-01-13'
    :return:类别的日榜TOP50
    """

    try:
        url = 'https://api.newrank.cn/api/sync/douyin/rank/v2/type/day/top50'
        layout = {
            'type': f'{search_type}',
            'date': f'{search_date}',
        }
        header = {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
            "Key": key}
        resp = requests.post(url=url, data=layout, headers=header)
        return resp.json()
    except Exception as e:
        print(e)


def douyin_week_top50(key, search_type, search_date):
    """
    获取指定新榜类别的日榜TOP50（仅可查2019年11月11号之前的日榜榜单）
    抖音账号新榜所有分类：娱乐、才艺、萌宠、搞笑、二次元、游戏、家居、
    美食、旅游、健康、企业、体育、教育、科技、汽车、情感、时尚、文化、社会
    程序调用:douyin_week_top50('家居','2022-01-31')
    """
    try:
        url = 'https://api.newrank.cn/api/sync/douyin/rank/v2/type/week/top50'
        layout = {
            'type': f'{search_type}',
            'date': f'{search_date}',
        }
        header = {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
            "Key": key}
        resp = requests.post(url, data=layout, headers=header)
        return resp.json()
    except Exception as e:
        print(e)


def douyin_day_account(key, search_uid, search_from, search_to, search_page, search_size):
    """
    获取新抖单个抖音号时间范围内的历史日榜
    douyin_day_account(key,'76725372134','2022-01-01','2022-01-31',1,20)
    :param key:
    :param search_uid:
    :param search_from:
    :param search_to:
    :param search_page: 非必要
    :param search_size: 非必要
    :return:
    """

    try:
        url = 'https://api.newrank.cn/api/sync/douyin/rank/v2/account/day'
        layout = {
            'uid': f'{search_uid}',
            'from': f'{search_from}',
            'to':f'{search_to}',
            'page':f'{search_page}',
            'size':f'{search_size}'
        }
        header = {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
            "Key": f'{key}'}
        resp = requests.post(url, data=layout, headers=header)
        return resp.json()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    key = '923047f0112e4c6d81476fa70'
    resp = douyin_day_account(key,'76725372134','2022-01-01','2022-01-30','1','20')
    print(resp)